/*
 * Copyright 2017. nekocode (nekocode.cn@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nekocode.dividerdrawable;

import ohos.agp.components.Component;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * @author nekocode (nekocode.cn@gmail.com)
 */
public class DividerDrawable extends ShapeElement {
    /**
     * 默认绘画笔的宽度
     */
    public static final int DEFAULT_STORKE_WIDTH = 1;

    /**
     * 默认的颜色
     */
    public static final int DEFAULT_COLOR = 0xFFCCCCCC;

    private final Paint paint;
    private DividerLayout layout;
    private int strokeWidth = DEFAULT_STORKE_WIDTH;
    private int[] wh = new int[2];
    private int[] layouted;
    private int width;
    private int height;
    private Component component;

    public DividerDrawable() {
        this(null);
    }

    public DividerDrawable(DividerLayout layout) {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE_STYLE);
        if (layout == null) {
            layout = new DividerLayout();
        }
        this.layout = layout;
    }

    /**
     * 获取绘画笔的宽度
     *
     * @return 宽度
     */
    public int getStrokeWitdth() {
        return strokeWidth;
    }

    /**
     * 设置绘画笔的宽度
     *
     * @param strokeWidth 宽度
     * @return 布局
     */
    public DividerDrawable setStrokeWidth(int strokeWidth) {
        this.strokeWidth = strokeWidth;
        return this;
    }

    /**
     * 设置绘画笔的宽度
     *
     * @param context       上下文
     * @param strokeWidthDp 宽度
     * @return 布局
     */
    public DividerDrawable setStrokeWidthDp(Context context, int strokeWidthDp) {
        this.strokeWidth = ScreenUtils.vp2px(context, strokeWidthDp);
        return this;
    }

    /**
     * 获取绘画颜色
     *
     * @return 颜色
     */
    public int getColor() {
        return paint.getColor().getValue();
    }

    /**
     * 设置绘画颜色
     *
     * @param color 颜色
     * @return 布局
     */
    public DividerDrawable setColor(int color) {
        paint.setColor(new Color(color));
        return this;
    }

    /**
     * 获取布局
     *
     * @return 布局 DividerLayout
     */
    public DividerLayout getLayout() {
        return layout;
    }

    /**
     * 设置绘画的宽度和高度
     *
     * @param width  宽度
     * @param height 高度
     */
    public void setWidthAndHight(int width, int height) {
        this.width = width;
        this.height = height;
    }

    /**
     * 传控件
     *
     * @param component 控件
     */
    public void setComponent(Component component) {
        this.component = component;
    }

    /**
     * 设置布局
     *
     * @param layout 布局
     */
    public void setLayout(DividerLayout layout) {
        this.layout = layout;
        notifyLayoutChanged();
    }

    /**
     * 刷新界面
     */
    public void notifyLayoutChanged() {
        DividerUtils.invalidateSelf(this);
    }

    @Override
    public void drawToCanvas(Canvas canvas) {
        final int width1 = width;
        final int height1 = height;

        if (layouted == null || wh[0] != width1 || wh[1] != height1) {
            layouted = layout.layout(width1, height1, strokeWidth);
            wh[0] = width1;
            wh[1] = height1;
        }

        paint.setStrokeWidth(strokeWidth);
        RectFloat rectFloat = new RectFloat(layouted[0], layouted[1], layouted[2], layouted[3]);
        canvas.drawRect(rectFloat, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

}
